package org.stvd.common.manager.factory;

import java.util.TimerTask;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.stvd.common.Consts;
import org.stvd.common.utils.ServletUtils;
import org.stvd.common.utils.ip.AddressUtil;
import org.stvd.common.utils.spring.SpringUtils;
import org.stvd.core.web.IPUtil;
import org.stvd.entities.logs.SysLoginInfo;
import org.stvd.entities.logs.SysOperLog;
import org.stvd.service.logs.SysLoginInfoService;
import org.stvd.service.logs.SysOperLogService;

import eu.bitwalker.useragentutils.UserAgent;

/**
 * @Title: AsyncFactory
 * @Description:异步工厂（产生任务用）
 * @author houzx
 * @date 2020年9月2日
 */
public class AsyncFactory {

    private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    /**
     * 记录登录信息
     * @param username 用户名
     * @param status 状态
     * @param message 消息
     * @param args 列表
     * @return 任务task
     */
    public static TimerTask recordLoginInfo(final String username, final String status, final String message,
            final Object... args) {
        HttpServletRequest request = ServletUtils.getRequest();
        final UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        final String ip = IPUtil.getIP(ServletUtils.getRequest());
        return new TimerTask() {
            @Override
            public void run() {
                String address = AddressUtil.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append(ip);
                s.append(address);
                s.append(username);
                s.append(status);
                s.append(message);
                // 打印信息到日志
                sys_user_logger.info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLoginInfo logininfor = new SysLoginInfo();
                logininfor.setUserName(username);
                logininfor.setIpaddr(ip);
                logininfor.setLoginLocation(address);
                logininfor.setBrowser(browser);
                logininfor.setOs(os);
                logininfor.setMsg(message);
                // 日志状态
                if (Consts.OPER_SUCCESS.equals(status)){
                    logininfor.setStatus(Consts.SUCCESS);
                }else if (Consts.OPER_FAIL.equals(status)){
                    logininfor.setStatus(Consts.FAIL);
                }
                // 插入数据
                SpringUtils.getBean(SysLoginInfoService.class).insertLoginInfo(logininfor);
            }
        };
    }

    /**
     * 操作日志记录
     * @param operLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOperLog(final SysOperLog operLog){
        return new TimerTask() {
            @Override
            public void run() {
                operLog.setOperLocation(AddressUtil.getRealAddressByIP(operLog.getOperIp()));
                SpringUtils.getBean(SysOperLogService.class).insertOperlog(operLog);
            }
        };
    }
}
